home *** CD-ROM | disk | FTP | other *** search
- /*
- File: AEHelpers.c
-
- Contains: Functions to help you when you are building and sending Apple events.
-
- Written by: Andy Bachorski
-
- Copyright: Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/21/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
-
- // Conditionals to setup the build environment the way we like it.
- #include "PrivateConditionals.h"
-
-
- //********** Universal Headers ****************************************
-
- #include <AERegistry.h>
- #include <AEObjects.h>
- #include <AEPackObject.h>
- #include <Gestalt.h>
- #include <Traps.h>
-
- #if UNIVERSAL_INTERFACES_VERSION >= 0x0300
- #include <FinderRegistry.h>
- #else
- #include "FinderRegistry.h"
- #endif
-
- //********** Project Headers ****************************************
-
- #include "AEHelpers.h"
- #include "ProcessHelpers.h"
-
-
- //********** Private Definitions ****************************************
-
- #define kFlagNotSet -1
-
-
- //******************************************************************************
-
-
- OSErr AEHMakeEventSelfTarget( const AEEventClass eventClass,
- const AEEventID eventID,
- AppleEvent *theEventPtr )
- {
- OSErr anErr = noErr;
-
- ProcessSerialNumber selfPSN = { 0, kCurrentProcess };
-
- anErr = AEHMakeEventProcessTarget( &selfPSN, eventClass, eventID, theEventPtr );
-
- return ( anErr );
- }//end AEHMakeEventSelfTarget
-
- //******************************************************************************
-
- pascal OSErr AEHMakeEventSignatureTarget( const OSType targetType,
- const OSType targetCreator,
- const AEEventClass eventClass,
- const AEEventID eventID,
- AppleEvent *theEventPtr )
- {
- OSErr anErr = noErr;
-
- ProcessSerialNumber psn = { kNoProcess, kNoProcess };
-
- anErr = FindProcessBySignature( targetType, targetCreator, &psn );
- if ( anErr == noErr )
- {
- anErr = AEHMakeEventProcessTarget( &psn, eventClass, eventID, theEventPtr );
- }
- return anErr;
- }//end AEHMakeEventSignatureTarget
-
- //******************************************************************************
-
- pascal OSErr AEHMakeEventProcessTarget( const ProcessSerialNumberPtr psnPtr,
- const AEEventClass eventClass,
- const AEEventID eventID,
- AppleEvent *theEventPtr )
- {
- OSErr anErr = noErr;
- AEDesc targetAppDesc = { typeNull, nil };
-
- anErr = AECreateDesc (typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &targetAppDesc);
-
- if ( anErr == noErr )
- {
- anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
- kAutoGenerateReturnID, kAnyTransactionID, theEventPtr);
- }
-
- AEDisposeDesc( &targetAppDesc );
-
- return anErr;
- }//end AEHMakeEventProcessTarget
-
- //******************************************************************************
-
- pascal OSErr AEHMakeEventTargetID( const TargetID *targetIDPtr,
- const AEEventClass eventClass,
- const AEEventID eventID,
- AppleEvent *theEventPtr )
- {
- OSErr anErr = noErr;
- AEDesc targetAppDesc = { typeNull, nil };
-
- anErr = AECreateDesc (typeTargetID, targetIDPtr, sizeof( TargetID ), &targetAppDesc);
-
- if ( anErr == noErr )
- {
- anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
- kAutoGenerateReturnID, kAnyTransactionID, theEventPtr);
- }
-
- AEDisposeDesc( &targetAppDesc );
-
- return anErr;
- }//end AEHMakeEventProcessTarget
-
- //******************************************************************************
-
- pascal OSErr AEHSendEventReturnSInt16( const AEIdleUPP idleProcUPP,
- const AppleEvent *theEvent,
- SInt16 *theValue )
- {
- OSErr anErr = noErr;
-
- if ( idleProcUPP == nil )
- {
- anErr = paramErr; // No idle function is an error, since we are expected to return a value
- }
- else
- {
- AppleEvent theReply = {typeNull, nil};
- AESendMode sendMode = kAEWaitReply;
-
- anErr = AESend( theEvent, &theReply, sendMode, kAENormalPriority,
- kNoTimeOut, idleProcUPP, nil );
- // Don't dispose of the event, it's not ours
- if ( anErr == noErr )
- {
- anErr = AEHGetHandlerError( &theReply );
-
- if ( !anErr && theReply.descriptorType != typeNull )
- {
- DescType actualType; // we don't care about these, because we are not asking
- Size actualSize; // typeWildcard, so we get an error if we dont' get what we want.
-
- anErr = AEGetParamPtr( theEvent, keyDirectObject, typeShortInteger,
- &actualType, theValue, sizeof( SInt16 ), &actualSize );
- }
- (void) AEDisposeDesc( &theReply );
- }
- }
- return anErr;
- }//end AEHSendEventReturnSInt16
-
- //******************************************************************************
-
- pascal OSErr AEHSendEventNoReturnValue( const AEIdleUPP idleProcUPP,
- const AppleEvent *theEvent )
- {
- OSErr anErr = noErr;
- AppleEvent theReply = { typeNull, nil };
- AESendMode sendMode;
-
- if ( idleProcUPP == nil )
- sendMode = kAENoReply;
- else
- sendMode = kAEWaitReply;
-
- anErr = AESend( theEvent, &theReply, sendMode, kAENormalPriority, kNoTimeOut, idleProcUPP, nil );
- if ( anErr == noErr && sendMode == kAEWaitReply )
- {
- anErr = AEHGetHandlerError( &theReply );
- }
- (void) AEDisposeDesc( &theReply );
-
- return anErr;
- }//end AEHSendEventNoReturnValue
-
- //******************************************************************************
-
- pascal OSErr AEHGetHandlerError( const AppleEvent *reply )
- {
- OSErr anErr = noErr;
- OSErr handlerErr;
-
- DescType actualType;
- long actualSize;
-
- if ( reply->descriptorType != typeNull ) // there's a reply, so there may be an error
- {
- OSErr getErrErr = noErr;
-
- getErrErr = AEGetParamPtr( reply, keyErrorNumber, typeShortInteger, &actualType,
- &handlerErr, sizeof( OSErr ), &actualSize );
-
- if ( getErrErr != errAEDescNotFound ) // found an errorNumber parameter
- {
- anErr = handlerErr; // so return it's value
- }
- }
- return anErr;
- }//end AEHGetHandlerError
-
- //**************************************************************************
-
- OSErr AEHExtractClassAndID ( const AppleEvent *theEventPtr, AEEventClass *eventClass, AEEventID *eventID )
- {
- DescType actualType;
- Size actualSize;
- OSErr anErr;
-
- anErr = AEGetAttributePtr( theEventPtr, keyEventClassAttr, typeType, &actualType,
- eventClass, sizeof( eventClass ), &actualSize );
- if ( anErr == noErr )
- {
- anErr = AEGetAttributePtr( theEventPtr, keyEventIDAttr, typeType, &actualType,
- eventID, sizeof( eventID ), &actualSize );
- }
- return ( anErr );
- }//end ExtractClassAndID
-
- //**************************************************************************
-
- pascal Boolean AEHSimpleIdleFunction( EventRecord *event,
- long *sleepTime,
- RgnHandle *mouseRgn )
- {
- #pragma unused( event )
- *sleepTime = 30;
- *mouseRgn = nil;
-
- return ( false );
- }//end AEHSimpleIdleFunction
-
- //******************************************************************************
-
- pascal Boolean GestaltAvailable( void )
- {
- OSErr anErr = noErr;
-
- static long gGestaltAvailable = kFlagNotSet;
-
- if ( gGestaltAvailable == kFlagNotSet )
- {
- if ( NGetTrapAddress(_Gestalt,kOSTrapType ) )
- {
- gGestaltAvailable = true;
- }
- else
- {
- gGestaltAvailable = false;
- }
- }
-
- return gGestaltAvailable;
- }//end GestaltAvailable
-
- //******************************************************************************
-
- pascal Boolean HasAppleEvents( void )
- {
- OSErr anErr = noErr;
-
- static long gHasAppleEvents = kFlagNotSet;
-
- if ( gHasAppleEvents == kFlagNotSet )
- {
- if ( GestaltAvailable() )
- {
- long response;
-
- if ( Gestalt( gestaltAppleEventsAttr, &response ) == noErr )
- {
- gHasAppleEvents = ( response & (1L << gestaltAppleEventsPresent) ) != 0;
- }
- }
- else
- {
- gHasAppleEvents = false;
- }
- }
-
- return gHasAppleEvents;
- }//end HasAppleEvents
-
- //******************************************************************************
-
- pascal Boolean FinderCallsAEProcess( void )
- {
- OSErr anErr = noErr;
-
- static long gFinderCallsAEProcess = kFlagNotSet;
-
- if ( gFinderCallsAEProcess == kFlagNotSet)
- {
- if ( GestaltAvailable() )
- {
- long response;
-
- if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
- {
- gFinderCallsAEProcess = ( response & (1L << gestaltFinderCallsAEProcess) ) != 0;
- }
- }
- else
- {
- gFinderCallsAEProcess = false;
- }
- }
-
- return gFinderCallsAEProcess;
- }//end FinderCallsAEProcess
-
- //******************************************************************************
-
- pascal Boolean FinderIsOSLCompliant( void )
- {
- OSErr anErr = noErr;
-
- static long gFinderIsOSLCompliant = kFlagNotSet;
-
- if ( gFinderIsOSLCompliant == kFlagNotSet )
- {
- if ( GestaltAvailable() )
- {
- long response;
-
- if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
- {
- gFinderIsOSLCompliant = ( response & (1L << gestaltOSLCompliantFinder) ) != 0;
- }
- }
- else
- {
- gFinderIsOSLCompliant = false;
- }
- }
-
- return gFinderIsOSLCompliant;
- }//end FinderIsOSLCompliant
-
- //******************************************************************************
-
- pascal Boolean FinderUsesIconFamily( void )
- {
- OSErr anErr = noErr;
-
- static long gFinderUsesIconFamily = kFlagNotSet;
-
- if ( gFinderUsesIconFamily == kFlagNotSet )
- {
- if ( GestaltAvailable() )
- {
- long response;
-
- if ( Gestalt( gestaltSystemVersion, &response ) == noErr )
- {
- gFinderUsesIconFamily = ( response != 0x00000800 );
- }
- }
- else
- {
- gFinderUsesIconFamily = true;
- }
- }
-
- return gFinderUsesIconFamily;
- }//end FinderUsesIconFamily
-
- //******************************************************************************
-
-